<template>
  <el-container class="app-container">
    <!--   顶部搜索栏   -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline" style="margin-left: 30px">
      <el-row>
        <el-col :span="20">
          <el-form-item label="用户名">
            <el-input
              v-model="queryParams.username"
              placeholder="请输入用户名"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="工号">
            <el-input
              v-model="queryParams.empId"
              placeholder="工号"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="queryParams.type" clearable placeholder="类型" style="width: 130px">
              <el-option label="员工" value="员工" />
              <el-option label="司机" value="司机" />
              <el-option label="经理" value="经理" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="fetchData(queryParams)">
              <i class="el-icon-zoom-in" />
              <span>查询</span>
            </el-button>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button type="success" round @click="dialogAddFormVisible=true">
            <i class="el-icon-circle-plus-outline" />
            <span>新建账号</span>
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <!--    表格    -->
    <el-main>
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="编号" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.accountId }}
          </template>
        </el-table-column>
        <el-table-column label="员工姓名" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column label="账号" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.empId }}
          </template>
        </el-table-column>
        <el-table-column label="员工类型" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
        <el-table-column label="注册时间" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.registryDate }}
          </template>
        </el-table-column>
        <el-table-column label="注册码" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.registryCode }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="created_at" label="操作" width="300" fixed="right">
          <template slot-scope="scope">
            <!--            <el-button type="primary" @click="openEdit(scope.$index)">流转</el-button>-->
            <!--            <el-button type="warning" @click="goToInfo(scope.row.ticketId)">详情</el-button>-->
<!--            <el-button type="danger" @click="remove(scope.row.accountId, scope.$index)">删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="创建账号" :visible.sync="dialogAddFormVisible" center>
        <el-form :model="accountInfoForm">
          <el-form-item label="选择员工" :label-width="formLabelWidth">
            <el-select v-model="accountInfoForm.empId" clearable placeholder="员工" filterable>
              <el-option v-for="(emp, index) in empList" :key="index" :label="emp.name" :value="emp.empId" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAddFormVisible=false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </div>
      </el-dialog>
    </el-main>
    <!--    底部分页    -->
    <el-footer>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="queryParams.current"
        :page-size="queryParams.size"
        style="display: flex;align-items: center;justify-content: center;"
        @current-change="changePage($event)"
      />
    </el-footer>
  </el-container>
</template>

<script>
import { getAccountList, register, remove } from '@/api/account'
import { getEmployeeList } from '@/api/table'

export default {
  name: 'AccountList',
  filters: {
    statusFilter(status) {

    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogFormVisible: false,
      dialogAddFormVisible: false,
      formLabelWidth: '100px',
      total: 0,
      accountList: [
        {
          accountId: '',
          username: '',
          registryDate: '',
          empId: '',
          type: '',
          registryCode: ''
        }
      ],
      accountInfoForm: {
        empId: ''
      },
      empList: [
        {
          empId: '',
          name: ''
        }
      ],
      queryParams: {
        size: 6,
        current: 1,
        username: '',
        empId: '',
        type: ''
      }
    }
  },
  created() {
    this.fetchData(this.queryParams)
    this.getEmployeeList()
  },
  methods: {
    fetchData(params) {
      const query = JSON.parse(JSON.stringify(params))
      for (const key in query) {
        if (query[key] === '') {
          delete query[key]
        }
      }
      this.listLoading = true
      getAccountList(query).then(response => {
        const { content } = response
        console.log(content)
        this.list = content.records
        this.total = content.total
        this.queryParams.current = content.current
        this.queryParams.size = content.size
        this.listLoading = false
      }).catch(error => {
        console.log(error)
      })
    },
    remove(accountId, index) {
      this.$confirm('此操作将永久删除编号为' + accountId + '的账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(accountId)
        remove({ accountId: accountId }).then(response => {
          const msg = response.content
          if (msg === -1) {
            this.$message({
              type: 'error',
              message: '该员工尚存在!'
            })
          } else {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.list.splice(index, 1)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    add() {
      register(this.accountInfoForm).then(response => {
        const { content } = response
        const code = content.registryCode
        this.$alert('创建成功，该账号注册码为：' + code, '成功', {
          confirmButtonText: '确定',
          callback: () => {
            this.list.push(content)
          }
        })
      })
    },
    changePage(event) {
      this.queryParams.current = event
      this.fetchData(this.queryParams)
    },
    getEmployeeList() {
      getEmployeeList({ size: 100 }).then(response => {
        const content = response.content.records
        this.empList = content
      })
    }
  }
}
</script>

<style scoped>

</style>
